﻿using System;
using System.Reflection;
using System.Web.Http;
using System.Web.Http.Cors;
using CVS.DAL;
using CVS.WebAPI.Mappers;
using log4net;

namespace CVS.WebAPI.Controllers
{
    public class TestsController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        // GET api/tests
        public IHttpActionResult GetByTaskId(int id)
        {
            try
            {
                var tests = DataRepository.GetTestsForTask(id);
                return Ok(TestsMapper.MapTests(tests));
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return InternalServerError(ex);
            }
        }

        // POST: api/Tests
        public void Post([FromBody] TestDefinition test)
        {
            try
            {
                DataRepository.AddTest(test);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        // PUT: api/Tests/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Tests/5
        public void Delete(TestDefinition test)
        {
            try
            {
                DataRepository.DeleteTest(test);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }
    }
}
